$(function(){
	//导航点击函数
	$.Huitab("#tab_demo .myTabBar span","#tab_demo .myTabCon","myCurrent","click","0");
	
})

function setBaoche(i){
	if (i == 0) {
		$("#baoche-out-city").hide();
	} else {
		$("#baoche-out-city").show();
	}
}

function setJieji(i){
	if (i == 0) {
		$("#jieji-flight").show();
		$("#jieji-airport").hide();
	} else {
		$("#jieji-flight").hide();
		$("#jieji-airport").show();
	}
}
function setSongji(i){
	if (i == 0) {
		$("#songji-flight").show();
		$("#songji-airport").hide();
	} else {
		$("#songji-flight").hide();
		$("#songji-airport").show();
	}
}

//包车游提交
function baocheResultSubmit(){
	var consumerClass = $('input[name="baocheRadio"]:checked ').val(); // 0或者1
	
	var useTime = $("#useTime").val();
	var useDuration = $("#useDuration").val();//仅对包车有效，其他类型请设置为1
	var productType = 3; //产品类型，1为接机，2为送机，3为包车游 ，4为单次接送
	var routeType = 1; //行程类型，1市内、2跨市单程、3跨市往返。
	
	if(consumerClass == 1){
		routeType = 3 //包车类型为跨城往返3
	}
	
	var fromCityName = $("#fromCity").val();
	var fromCityCode = $("#auto_fromCity").val();
	var fromCityID = 0; //出发城市ID
	var fromAddress = $("#fromAddress").val();
	var fromLongitude = $("#fromLongitude").val();
	var fromLatitude = $("#fromLatitude").val();
	
	var toCityID = 0; //出发城市ID
	var toAddress = $("#toAddress").val();
	var toLongitude = $("#toLongitude").val();
	var toLatitude = $("#toLatitude").val();
	
	var data = {}; //请求参数对象
	
	if(fromCityName == "" || fromCityCode == ""){
		layer.open({
            title: '提示',
            content: '请选择城市！ '
        });
        $("#fromCity").focus();
        return;
	}
	if(fromAddress == '' || fromLongitude ==  '' || fromLatitude == ''){
		layer.open({
            title: '提示',
            content: '请选择上车地址！ '
        });
        $("#fromAddress").focus();
        return;
	}
	if(consumerClass == 1){
		if(toAddress == '' || toLongitude ==  '' || toLatitude == ''){
			layer.open({
	            title: '提示',
	            content: '请选择下车地址！ '
	        });
	        $("#toAddress").focus();
	        return;
		}
		
		data.toAddress = toAddress;
		data.toLongitude = toLongitude;
		data.toLatitude = toLatitude;
	}
	if(useTime == ''){
		layer.open({
	        title: '提示',
	        content: '请选择用车时间！ '
	    });
	    $("#useTime").focus();
	    return;
	}
	
	data.useTime = useTime ;
	data.useDuration = useDuration ;
	data.productType = productType ;
	data.routeType = routeType ;
	data.fromCityCode = fromCityCode ;
	data.fromCityID = fromCityID ;
	data.fromAddress = fromAddress ;
	data.fromLongitude = fromLongitude ;
	data.fromLatitude = fromLatitude ;
	
	//异步查询接机车型
	submitSearchCartype(data);
}

//接机提交
function jiejiResultSubmit(){
	var consumerClass = $('input[name="jiejiRadio"]:checked').val();
	var outBoundDate = $("#outBoundDate2").val();
	var useDuration = 1;	//仅对包车有效，其他类型请设置为1
	var productType = 1; //产品类型，1为接机，2为送机，3为包车游 ，4为单次接送
	var routeType = 1; //行程类型，1市内、2跨市单程、3跨市往返。
	//暂未支持航班查询
	var flightNum = $("#flightNum2").val();
	
	var useTime = $("#useTime2").val(); //开始用车时间（当地时间）
	
	var toCityID = 0;
	var toAddress = $("#toAddress2").val();
	var toLongitude = $("#toLongitude2").val();
	var toLatitude = $("#toLatitude2").val();
	
	var airportName = $("#airport2").val();
	var airportCode = $("#auto_airport2_code").val();
	var fromCityID = 0; //出发城市ID
	var fromAddress = $("#auto_airport2_name").val();
	var fromLongitude = $("#auto_airport2_longitude").val();
	var fromLatitude = $("#auto_airport2_latitude").val();
	
	var data = {}; //请求参数
	
	if(consumerClass == 0){
		if(outBoundDate == ''){
			layer.open({
		        title: '提示',
		        content: '请选择起飞时间！ '
		    });
		    $("#outBoundDate").focus();
		    return;
		}
		if(flightNum == ''){
			layer.open({
		        title: '提示',
		        content: '请输入航班号！ '
		    });
		    $("#flightNum2").focus();
		    return;
		}
		if(!checkFlightNum(flightNum)){
			layer.open({
		        title: '提示',
		        content: '请正确输入航班号！ '
		    });
		    $("#flightNum2").focus();
		    return;
		}
		data.flightNum = flightNum;
	}
	
	if(airportName == "" || airportCode == '' || fromAddress == '' || fromLongitude =='' || fromLatitude==''){
		layer.open({
	        title: '提示',
	        content: '请选择接机机场！ '
	    });
	    $("#airport2").focus();
	    return;
	}
		
	if(useTime == ''){
		layer.open({
	        title: '提示',
	        content: '请选择用车时间！ '
	    });
	    $("#useTime2").focus();
	    return;
	}
	if(toAddress == '' || toLongitude == '' || toLatitude == ''){
		layer.open({
	        title: '提示',
	        content: '请选择送达地址！ '
	    });
	    $("#toAddress2").focus();
	    return;
	}
	
	data.useTime = useTime ;
	data.useDuration = useDuration ;
	data.productType = productType ;
	data.routeType = routeType ;
	data.airportCode = airportCode;
	data.fromCityID = fromCityID ;
	data.fromAddress = fromAddress ;
	data.fromLongitude = fromLongitude ;
	data.fromLatitude = fromLatitude ;
	data.toCityID = toCityID ;
	data.toAddress = toAddress;
	data.toLongitude = toLongitude;
	data.toLatitude = toLatitude;
	
//	var data = { "useTime":useTime,
//			"useDuration":useDuration,
//			"productType":productType,
//			"routeType":routeType,
//			"airportCode":airportCode,
//			"fromCityID":fromCityID,
//			"fromAddress":fromAddress,
//			"fromLongitude":fromLongitude,
//			"fromLatitude":fromLatitude,
//			"toCityID":toCityID,	
//			"toAddress":toAddress,
//			"toLongitude":toLongitude,
//			"toLatitude":toLatitude
//		};
	
	//异步查询接机车型
	submitSearchCartype(data);
}

//送机提交
function songjiResultSubmit(){
	
	var consumerClass = $('input[name="songjiRadio"]:checked ').val();
	var outBoundDate = $("#outBoundDate3").val();
	
	
	var useTime = $("#useTime3").val(); //开始用车时间（当地时间）
	var useDuration = 1;	//仅对包车有效，其他类型请设置为1
	var productType = 2; //产品类型，1为接机，2为送机，3为包车游 ，4为单次接送
	var routeType = 1; //行程类型，1市内、2跨市单程、3跨市往返。
	//暂未支持航班查询
	var flightNum = $("#flightNum3").val();
	
	var fromCityID = 0; //出发城市ID
	var fromAddress = $("#fromAddress3").val();
	var fromLongitude = $("#fromLongitude3").val();
	var fromLatitude = $("#fromLatitude3").val();
	var airportName = $("#airport3").val();
	var airportCode = $("#auto_airport3_code").val();
	var toCityID = 0;
	var toAddress = $("#auto_airport3_name").val();
	var toLongitude = $("#auto_airport3_longitude").val();
	var toLatitude = $("#auto_airport3_latitude").val();
	
	var data = {}; //请求参数
	
	if(consumerClass == 0){
		if(outBoundDate == ''){
			layer.open({
		        title: '提示',
		        content: '请选择起飞时间！ '
		    });
		    $("#outBoundDate3").focus();
		    return;
		}
		if(flightNum == ''){
			layer.open({
		        title: '提示',
		        content: '请输入航班号！ '
		    });
		    $("#flightNum3").focus();
		    return;
		}
		if(!checkFlightNum(flightNum)){
			layer.open({
		        title: '提示',
		        content: '请正确输入航班号！ '
		    });
		    $("#flightNum3").focus();
		    return;
		}
		data.flightNum = flightNum;
	}else{
		if(airportName == "" || airportCode == '' || toAddress == '' || toAddress =='' || toAddress==''){
			layer.open({
		        title: '提示',
		        content: '请选择送机机场！'
		    });
		    $("#airport3").focus();
		    return;
		}
	}
	if(useTime == ''){
		layer.open({
	        title: '提示',
	        content: '请选择用车时间！ '
	    });
	    $("#useTime3").focus();
	    return;
	}
	if(fromAddress == '' || fromLongitude == '' || fromLatitude == ''){
		layer.open({
	        title: '提示',
	        content: '请选择出发地址！ '
	    });
	    $("#fromAddress3").focus();
	    return;
	}
	
	data.useTime = useTime ;
	data.useDuration = useDuration ;
	data.productType = productType ;
	data.routeType = routeType ;
	data.airportCode = airportCode;
	data.fromCityID = fromCityID ;
	data.fromAddress = fromAddress ;
	data.fromLongitude = fromLongitude ;
	data.fromLatitude = fromLatitude ;
	data.toCityID = toCityID ;
	data.toAddress = toAddress;
	data.toLongitude = toLongitude;
	data.toLatitude = toLatitude;
	
	//异步查询送机车型
	submitSearchCartype(data);
}

// 单次接送提交
function jiesongResultSubmit(){
	var consumerClass = $('input[name="jiesongRadio"]:checked ').val();
	
	var useTime = $("#useTime4").val();
	var useDuration = 1;	//仅对包车有效，其他类型请设置为1
	var productType = 4; //产品类型，1为接机，2为送机，3为包车游 ，4为单次接送
	var routeType = 1; //行程类型，1市内、2跨市单程、3跨市往返。
	
	var fromCityName = $("#fromCity4").val();
	var fromCityCode = $("#auto_fromCity4").val();
	var fromCityID = 0; //出发城市ID
	var fromAddress = $("#fromAddress4").val();
	var fromLongitude = $("#fromLongitude4").val();
	var fromLatitude = $("#fromLatitude4").val();
	
	var toCityID = 0; //出发城市ID
	var toAddress = $("#toAddress4").val();
	var toLongitude = $("#toLongitude4").val();
	var toLatitude = $("#toLatitude4").val();
	
	var data = {}; //请求参数
	
	if(fromCityName == '' || fromCityCode == ""){
		layer.open({
	        title: '提示',
	        content: '请选择用车城市！ '
	    });
	    $("#fromCity4").focus();
	    return;
	}
	if(fromAddress == '' || fromLongitude == '' || fromLatitude == ''){
		layer.open({
	        title: '提示',
	        content: '请选择上车地址！ '
	    });
	    $("#fromAddress4").focus();
	    return;
	}
	if(toAddress == '' || toLongitude == '' || toLatitude == ''){
		layer.open({
	        title: '提示',
	        content: '请选择下车地址！ '
	    });
	    $("#toAddress4").focus();
	    return;
	}
	if(useTime == ''){
		layer.open({
	        title: '提示',
	        content: '请选择用车时间！ '
	    });
	    $("#useTime4").focus();
	    return;
	}
	
	data.useTime = useTime ;
	data.useDuration = useDuration ;
	data.productType = productType ;
	data.routeType = routeType ;
	data.fromCityCode = fromCityCode;
	data.fromCityID = fromCityID ;
	data.fromAddress = fromAddress ;
	data.fromLongitude = fromLongitude ;
	data.fromLatitude = fromLatitude ;
	data.toCityID = toCityID ;
	data.toAddress = toAddress;
	data.toLongitude = toLongitude;
	data.toLatitude = toLatitude;
	
	//异步查询送机车型
	submitSearchCartype(data);
	
}

function submitSearchCartype(data){
	jQuery('#body').showLoading({
	      'label' : '正在查询'
	});
	
	//隐藏上次查询的车型列表
	$(".queryResult").hide();
	$(".estimatedinfo").hide();
	$.ajax({
		type : "post",
		url : "../api/intcar/queryPrice",
		data : data,
		success : function(resulet){
			jQuery('#body').hideLoading();
			resuletJson = jQuery.parseJSON(resulet);
			if(resuletJson.success){
				//展示车型
				showCarType(resuletJson.obj);
				$(".queryResult").show();
				if(data.productType ==1 || data.productType ==2 || data.productType == 4 ){
					$(".estimatedinfo").show();
				}
			} else {
				layer.open({
			        title: '提示',
			        content: resuletJson.message
			    });
			    return;
			}
		},
		error : function(resulet){
			jQuery('#body').hideLoading();
			layer.open({
		        title: '提示',
		        content: '系统出错！'
		    });
		    return;
		}
	});
}

function showCarType(obj) {

    var html = '';
    
    $("#estimatedDistance").html((obj.estimatedDistance / 1000).toFixed(2));
    $("#estimatedTime").html(obj.estimatedTime);

    $.each(obj.carTypeList, function (index, item) {
        html += '<div class="product">';
        html += '<span class="pricetag"></span>';
        html += '<div class="imagediv">';
        html += '<span></span>';
        html += '<img src="' + item.imageURL + '" />';
        html += '</div>';
        html += '<div class="carcontent">';
        html += '<span class="cartitle">';
        html += item.name + '<div class="passenger">&emsp;&nbsp;&nbsp;<img src="../static/eth/images/passenger.png" /> X' + item.maxPassenger + '&emsp;&nbsp;<img src="../static/eth/images/passenger.png" /> X' + item.maxLuggage + ' </div>';
        html += '</span>';
        html += '<span class="carinfo">';
        html += item.carBrands;
        html += '</span>';
        html += '<ul class="tags">';
        if (item.isChineseService == "1") {
            html += '<li>中文司导</li>';
        }
        html += '<li>晚点免费等</li>';
        html += '<li>中文客服</li>';
        html += '</ul>';
        html += '<i></i>';
        html += '</div>';
        html += '<div class="buyprice">';
        html += '<div class="price">';
        html += '<i>￥</i><b>' + item.setPrice + '</b> / 车';
        html += '</div>';
        html += '<input type="button" class="btnBuy" data-carTypeID="' + item.carTypeID + '" value="立即预订"/>';
        html += '</div>';
        html += '</div>';
    });
    $("#carList").html(html);
    
    // 热门产品展示
    var hotHtml = '';
    hotHtml += '<div class="guessfavourite">';
    hotHtml += '<span class="title">热销产品</span>';
    hotHtml += '<div class="product">';
    hotHtml += '<div class="imagediv">';
    hotHtml += '<img src="http://www.yitu8.net/userfiles/product/142/142_20170124092123.png">';
    hotHtml += '<span class="producttitle">';
    hotHtml += '<a href="/detail/pid/102524.htm" target="_blank" class="hotlink">【单次接送】韩国首尔市内中文包车单次接送服务</a>';
    hotHtml += '</span>';
    hotHtml += '<div class="price">';
    hotHtml += '￥<b>300.00</b> 起/车';
    hotHtml += '<div class="sales">已售78051</div>';
    hotHtml += '<div class="titledash">&nbsp;</div>';
    hotHtml += '</div>';
    $("#showRight").html(hotHtml);
}


// 航班号校验
function checkFlightNum(flightNum){
	if(flightNum == "" || flightNum == undefined){
		return false;
	}
	flightNum = flightNum.toLocaleUpperCase(); 
	reg =/^[A-Z]{2}[0-9]{3,4}$/img;
	if(!reg.test(flightNum)){
		return false;
	}
	return true;
}